package org.dtrd.modules.record.entity.bean;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.common.util.DateUtils;
import org.dtrd.config.system.Constant;
import org.dtrd.modules.record.entity.po.DtrdEntRdSigninBloodPressure;

import java.text.ParseException;

/**
 * @Author QR
 * @Date 2021/10/29 12:24 PM
 */
@ApiModel("每日血压信息对象")
@Data
@Slf4j
public class BloodPressureDailyInfo {

    @ApiModelProperty("患者id")
    private Integer patientId;

    @ApiModelProperty("记录时间")
    private String recordTime;

    @ApiModelProperty(value = "记录类型", notes = "1 -晨起 2-睡前 3-随机 ")
    private Integer recordType;

    @ApiModelProperty("收缩压")
    private Integer high;
    @ApiModelProperty("收缩压")
    private Integer low;
    @ApiModelProperty("心率")
    private Integer heart;

    @ApiModelProperty("晨起收缩压")
    private Integer morningHigh;

    @ApiModelProperty("晨起舒张压")
    private Integer morningLow;

    @ApiModelProperty("晨起心率")
    private Integer morningHeartRate;

    @ApiModelProperty("睡前收缩压")
    private Integer nightHigh;

    @ApiModelProperty("睡前舒张压")
    private Integer nightLow;

    @ApiModelProperty("睡前心率")
    private Integer nightHeartRate;

    @ApiModelProperty("备注")
    private String remark;

    public BloodPressureDailyInfo parseFromPo(DtrdEntRdSigninBloodPressure po) {
        if (po.getBloodPressureType().equals(Constant.BloodPressureRecordType.MORNING.getType())) {
            this.setMorningHigh(po.getBloodPressureHighPressure());
            this.setMorningLow(po.getBloodPressureLowVoltage());
            this.setMorningHeartRate(po.getBloodPressureHeartRate());
        }
        if (po.getBloodPressureType().equals(Constant.BloodPressureRecordType.EVENING.getType())) {
            this.setNightHigh(po.getBloodPressureHighPressure());
            this.setNightLow(po.getBloodPressureLowVoltage());
            this.setNightHeartRate(po.getBloodPressureHeartRate());
        }
        return this;
    }

    public DtrdEntRdSigninBloodPressure toMorningRecordPo(DtrdEntRdSigninBloodPressure morningRecord) {
        return transferToPo(morningRecord, morningHigh, morningLow, morningHeartRate, Constant.BloodPressureRecordType.MORNING.getType());
    }

    public DtrdEntRdSigninBloodPressure toNightRecordPo(DtrdEntRdSigninBloodPressure nightRecord) {
        return transferToPo(nightRecord, nightHigh, nightLow, nightHeartRate, Constant.BloodPressureRecordType.EVENING.getType());
    }

    private DtrdEntRdSigninBloodPressure transferToPo(DtrdEntRdSigninBloodPressure po, Integer highPressure, Integer lowPressure, Integer heartRate, int recordType) {
        po.setPatientId(patientId);
        try {
            po.setRecordDate(DateUtils.parseDate(recordTime, DateUtils.date_sdf.get().toPattern()));
            po.setRecordTime(DateUtils.parseDate(recordTime, DateUtils.datetimeFormat.get().toPattern()));
        } catch (ParseException e) {
            log.error("BloodPressureDailyInfo.recordTime 转换失败: {}" + recordTime);
            e.printStackTrace();
        }
        po.setBloodPressureHighPressure(highPressure);
        po.setBloodPressureLowVoltage(lowPressure);
        po.setBloodPressureHeartRate(heartRate);
        po.setBloodPressureType(recordType);
        return po;
    }

}
